import requests
from bs4 import BeautifulSoup
import time
from selenium import webdriver

def k3_hn_yesterday():
    '''
    :return:返回前一天的期号和开奖号码列表
    '''
    data = []
    url = 'http://k3.icaile.com/hnk3.php?op=k3zs&num=zt'
    # header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'}
    # res = requests.get(url,headers = header)
    #开奖信息使用JS渲染，使用PhantomJS来获取
    driver = webdriver.PhantomJS()
    driver.get(url)
    time.sleep(3)

    soup = BeautifulSoup(driver.page_source,'lxml')

    lssue_list = soup.find_all('td',class_='chart-bg-qh')
    for i,lssue in enumerate(lssue_list):
        lssue = lssue.get_text()   #期号
        # print(i)
        code_0 =soup.find('td',id='%s_0'%i).get_text() #第一个开奖数
        code_1 =soup.find('td',id='%s_1'%i).get_text() #第二个开奖数
        code_2 =soup.find('td',id='%s_2'%i).get_text() #第三个开奖数
        data1 = [lssue,code_0,code_1,code_2]
        # print(lssue,code_0,code_1,code_2)
        # print('-------')
        data.append(data1)
    driver.close()
    return data


def k3_hn_today():
    '''
    :return:返回当天的期号和开奖号码列表
    '''
    data = []
    url = 'http://k3.icaile.com/hnk3.php?op=k3zs&num=jt'
    # header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'}
    # res = requests.get(url,headers = header)
    #开奖信息使用JS渲染，使用PhantomJS来获取
    driver = webdriver.PhantomJS()
    driver.get(url)
    time.sleep(3)

    soup = BeautifulSoup(driver.page_source,'lxml')

    lssue_list = soup.find_all('td',class_='chart-bg-qh')
    for i,lssue in enumerate(lssue_list):
        lssue = lssue.get_text()   #期号
        # print(i)
        code_0 =soup.find('td',id='%s_0'%i).get_text() #第一个开奖数
        code_1 =soup.find('td',id='%s_1'%i).get_text() #第二个开奖数
        code_2 =soup.find('td',id='%s_2'%i).get_text() #第三个开奖数
        data1 = [lssue,code_0,code_1,code_2]
        # print(lssue,code_0,code_1,code_2)
        # print('-------')
        data.append(data1)
    driver.close()
    return data



if __name__ =='__main__':
    data = k3_hn_today()
    print(data)
    print(data[-1][0])
    for li in data:
        print(li[0])

